using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;
using System.Collections.Generic;

namespace Sinbiota.Web.Controllers
{
    public class DepositaryUnitController : BaseController
    {
        private readonly IDepositaryUnitPersistenceServices _depositaryUnitPersistenceServices;

        public DepositaryUnitController(IDepositaryUnitPersistenceServices depositaryUnitPersistenceServices, IUserPersistenceServices userPersistenceServices)
        {
            _depositaryUnitPersistenceServices = depositaryUnitPersistenceServices;
        }

        //
        // GET: /DepositaryUnit/
        [ImportMessagesFromTempData]
        public ActionResult Index()
        {
            return View(_depositaryUnitPersistenceServices.FindAll().Where(x => x.DepositaryUnitRecords.Count > 0).OrderBy(x => - x.DepositaryUnitRecords.Count));
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(_depositaryUnitPersistenceServices.FindByNameLike(collection[SearchBoxName]));
        }

        //
        // GET: /DepositaryUnit/Details/5
        [ImportMessagesFromTempData]
        public ActionResult Details(int id)
        {
            var depositaryUnit = _depositaryUnitPersistenceServices.FindById(id);
            return depositaryUnit == null ? View("NotFound") : View(depositaryUnit);
        }

        //
        // GET: /DepositaryUnit/Create
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /DepositaryUnit/Create
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Create(DepositaryUnit depositaryUnit)
        {
            var newDepositaryUnit = new DepositaryUnit();
            TryUpdateModel(newDepositaryUnit);
            if (ModelState.IsValid)
            {
                if (_depositaryUnitPersistenceServices.FindByExactName(newDepositaryUnit.Name) != null)
                {
                    // Add error in the relevant field (name):
                    ModelState.AddModelError("Name", Messages.DepositaryUnitAlreadyExists);
                    // Throw an validation exception (must be of this type)
                    throw new InvalidOperationException();
                }
                _depositaryUnitPersistenceServices.Save(newDepositaryUnit);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(depositaryUnit);
        }

        //
        // GET: /DepositaryUnit/Edit/5
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Edit(int id)
        {
            var depositaryUnit = _depositaryUnitPersistenceServices.FindById(id);
            return depositaryUnit == null ? View("NotFound") : View(depositaryUnit);
        }

        //
        // POST: /DepositaryUnit/Edit/5
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Edit(int id, DepositaryUnit depositaryUnit)
        {
            var editDepositaryUnit = _depositaryUnitPersistenceServices.FindById(id);
            TryUpdateModel(editDepositaryUnit);
            if (ModelState.IsValid)
            {
                if (_depositaryUnitPersistenceServices.FindByExactName(editDepositaryUnit.Name) != null)
                {
                    // Add error in the relevant field (name):
                    ModelState.AddModelError("Name", Messages.DepositaryUnitAlreadyExists);
                    // Throw an validation exception (must be of this type)
                    throw new InvalidOperationException();
                }
                _depositaryUnitPersistenceServices.Update(editDepositaryUnit);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(depositaryUnit);
        }

        //
        // GET: /DepositaryUnit/Delete/5
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Delete(int id)
        {
            var depositaryUnit = _depositaryUnitPersistenceServices.FindById(id);
            return depositaryUnit == null ? View("NotFound") : View(depositaryUnit);
        }

        //
        // POST: /DepositaryUnit/Delete/5
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var deleteDepositaryUnit = _depositaryUnitPersistenceServices.FindById(id);
            if (deleteDepositaryUnit == null) return View("NotFound");
            _depositaryUnitPersistenceServices.Delete(deleteDepositaryUnit);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }

        //AJAX
        public JsonResult AutoComplete(string term, int count = 30)
        {
            return Json(_depositaryUnitPersistenceServices.FindByNameLike(term).Select(t => t.Name).Take(count), JsonRequestBehavior.AllowGet);
        }
            
    }
}
